/**
 *  Eclipse Public License 1.0
 */
package org.fireblade.easysms;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.Map;

import android.app.Service;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.provider.Contacts;

/**
 * Action for getting a contact picture as png
 */
public class HttpActionGetPicture {

  /**
   * @param values
   * @param out
   * @param service the context/service
   * @throws Exception
   */
  public static void process(Service service, Map<String, String> values, OutputStream out) throws Exception {
    if (null != values.get("id")) {
      out.write("HTTP/1.1 200 OK\r\n".getBytes());
      out.write("Cache-Control: max-age=7776000, public\r\n".getBytes());
      out.write("Last-Modified: Sat, 26 Dec 2009 15:25:10 GMT\r\n".getBytes());
      String id = values.get("id").toString();
      byte[] bytes = getPicture(service, id);
      if (null != bytes) {
        out.write(bytes);
      }
    }
  }


  /**
   * @param writer
   */
  private static byte[] getPicture(Service service, String id) throws Exception {
    Bitmap bm = null;
    bm = BitmapFactory.decodeResource(service.getResources(), R.drawable.icon, null);
    if (null != bm) {
      ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
      bm.compress(CompressFormat.PNG, 100, byteOut);
      return byteOut.toByteArray();
    }
    return null;
  }

}
